<template>
  <div class="container-content">
    <el-row>
      <el-col :span="24">
        <h3 class="panel-title">编辑工程合同</h3>
      </el-col>
    </el-row>
    <el-form
      ref="formRef"
      :model="formObject"
      label-width="100px"
      label-position="left"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="合同类型">
            <el-select
              v-model="formObject.type"
              placeholder="请选择"
              style="width: 100%"
              disabled
              @change="getContractTypeOptions"
            >
              <el-option
                v-for="item in typeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同模版">
            <el-select
              v-model="contractType"
              placeholder="请选择"
              style="width: 100%"
              disabled
              @change="getContractContent"
            >
              <el-option
                v-for="item in contractTypeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-card shadow="never">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="合同名称">
              <el-input v-model="formObject.contractName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="合同编号">
              <el-input v-model="formObject.contractNumber"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户Id">
              <el-input v-model="formObject.customerId"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="合同金额">
              <el-input v-model="formObject.money"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="合同金额大写" label-width="110px">
              <el-input v-model="formObject.moneyUpper"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="税率">
              <el-input v-model="formObject.taxRate"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="合同执行状态">
              <el-input v-model="formObject.status"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="签订日期">
              <el-date-picker
                v-model="formObject.signTime"
                type="date"
                placeholder="选择日期"
                style="width: 100%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8">
            <el-form-item label="税率">
              <el-input v-model="formObject.taxRate"></el-input>
            </el-form-item>
          </el-col> -->
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="甲方名称">
              <el-input v-model="formObject.partyA"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="甲方联系人名称" label-width="110px">
              <el-input v-model="formObject.partyAContactName"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="甲方联系邮箱">
              <el-input v-model="formObject.partyAEmail"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="甲方联系电话">
              <el-input v-model="formObject.partyATel"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="乙方名称">
              <el-input v-model="formObject.partyB"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="乙方联系人名称" label-width="110px">
              <el-input v-model="formObject.partyBContactName"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="乙方联系邮箱">
              <el-input v-model="formObject.partyBEmail"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="乙方联系电话">
              <el-input v-model="formObject.partyBTel"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="formObject.remarks" type="textarea"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>
      <Editor
        ref="editor"
        editor-id="editor"
        height="500px"
        :template-content="formObject.contractContent"
      />
      <el-form-item label-width="0px">
        <el-button type="primary" @click="onSubmit">保存</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {
  getList,
  editProjectList,
  getDetailProject,
} from "@/api/system/contract";
import Editor from "../../editor/index.vue";
export default {
  components: {
    Editor,
  },
  data() {
    return {
      contractType: "",
      contractTypeOptions: [],
      formObject: {
        contractContent: "",
        contractName: "",
        contractNumber: "",
        createBy: "",
        createTime: "",
        customerId: "",
        id: "",

        money: "",
        moneyUpper: "",
        params: {},
        partyA: "",
        partyAContactName: "",
        partyAEmail: "",
        partyATel: "",
        partyB: "",
        partyBContactName: "",
        partyBEmail: "",
        partyBTel: "",
        projectId: "",
        remark: "",
        remarks: "",
        salesUserId: "",
        searchValue: "",
        signTime: "",
        status: "",
        taxMoney: "",
        taxRate: "",
        tenderId: "",
        type: "",
        updateBy: "",
        updateTime: "",
      },
      typeOptions: [
        { label: "EPC", value: 181 },
        { label: "EMC", value: 182 },
        { label: "分包", value: 184 },
      ],
    };
  },
  methods: {
    getContractTypeOptions(val) {
      getList({
        typeId: val,
      }).then((res) => {
        if (res.code === 200) {
          this.contractTypeOptions = res.rows.map((item) => {
            return {
              id: item.id,
              value: item.id,
              label: item.tempName,
              content: item.contractTempContent,
            };
          });
        }
      });
    },
    getContractContent(val) {
      this.formObject.contractContent = this.contractTypeOptions.find(
        (item) => item.id == val
      ).content;
    },
    onSubmit() {
      editProjectList(this.formObject).then((res) => {
        if (res.code === 200) {
          this.$message({
            message: "修改成功！",
            type: "success",
          });
          this.$store.dispatch("tagsView/delAndPush", {
            name: "EditProject",
            path: "/Contract/ContractProject",
          });
        }
      });
    },
    getData(id) {
      getDetailProject(id).then((res) => {
        this.formObject = res.data;
        this.getContractTypeOptions(res.data.type);
      });
    },
  },
  created() {
    this.getData(this.$route.query.id);
  },
};
</script>
<style lang="scss" scoped></style>
